﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;
using System.Text.RegularExpressions;

namespace RedSand.Site.Controllers
{
    public class RoleController : BaseController
    {
        public ActionResult Manager()
        {
            return View(new ManagerModel { Title = "Role manager" });
        }

        [HttpGet, AjaxOrChildAction]
        public ActionResult List()
        {
            return PartialView(RsDataManager.GetList<RoleInfo>("RoleGetAll"));
        }
        [HttpGet, AjaxOrChildAction]
        public ActionResult Create()
        {
            return PartialView();
        }
        [HttpPost, AjaxOrChildAction]
        public ActionResult Create(RoleInfo info)
        {
            if (ModelState.IsValid)
            {
                if (RsDataManager.Save("RoleSave", info)>0)
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                return AjaxMessage(Message.CreateError);
            }
            return AjaxModelError();
        }
        [HttpGet, AjaxOrChildAction]
        public ActionResult Edit(int id)
        {
            return PartialView(RsDataManager.GetInfo<RoleInfo>("RoleGetInfo", "RoleID", id));
        }
        [HttpPost, AjaxOrChildAction]
        public ActionResult Edit(RoleInfo info)
        {
            if (ModelState.IsValid)
            {
                if (RsDataManager.Save("RoleSave", info)>0)
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                return AjaxMessage(Message.UpdateError);
            }
            return AjaxModelError();
        }
        [AjaxOrChildAction]
        public ActionResult Delete(int id)
        {
            if (RsDataManager.Delete("RoleDelete", "RoleID", id))
            {
                AddMessage(Message.DeleteSuccess);
                return RedirectToAction("List");
            }
            return AjaxMessage(Message.DeleteError);            
        }
        public MvcHtmlString GetRoleList(bool? validate, string name, int? selectedValue)
        {
            List<RoleInfo> list = RsDataManager.GetList<RoleInfo>("RoleGetAll");
            TagBuilder select = new TagBuilder("select");
            select.Attributes["name"] = name ?? "RoleID";
            select.GenerateId(name ?? "RoleID");
            if (validate.HasValue && validate.Value)
            {
                select.Attributes["data-val"] = "true";
                select.Attributes["data-val-required"] = "The " + (name ?? "RoleID") + " field is required.";
            }
            
            return MvcHtmlString.Create(select.ToString());
        }
        public ActionResult GetRoleForAction(int actionID, bool enable)
        {
            ViewBag.ID = actionID;
            ViewBag.Enable = enable;
            return PartialView("CheckBoxList", RsDataManager.GetList<RoleCheckListModel>("RoleGetRoleForAction", new { ActionID = actionID }));
        }
        public ActionResult GetRoleForUser(int userID, bool enable)
        {
            ViewBag.ID = userID;
            ViewBag.Enable = enable;
            return PartialView("CheckBoxList", RsDataManager.GetList<RoleCheckListModel>("RoleGetRoleForUser", new { UserID = userID }));
        }
    }
}
